package cn.weekimwee.map3d;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.util.Pair;
import android.view.View;
import android.widget.TextView;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.BitmapDescriptor;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.LatLngBounds;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MyLocationStyle;
import com.amap.api.maps.model.Polyline;
import com.amap.api.maps.model.PolylineOptions;
import com.amap.api.maps.utils.SpatialRelationUtil;
import com.amap.api.maps.utils.overlay.SmoothMoveMarker;
import com.amap.api.services.core.LatLonPoint;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Main3Activity extends AppCompatActivity {
    TextView startButton,stopButton,huifuButton;
    MapView mapView;
    AMap aMap;
    Polyline polyline;
    SmoothMoveMarker smoothMarker;
    LatLng passPoint3 = new LatLng(29.860964,121.592813);
    LatLng startPoint = new LatLng(29.85778,121.5894);
    LatLng passPoint1 = new LatLng(29.859977,121.580228);
    LatLng passPoint2 = new LatLng(29.864127,121.581387);
    LatLng endPoint =new LatLng(29.857979,121.579363);
    List<LatLng> list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        mapView = findViewById(R.id.map);
        mapView.onCreate(savedInstanceState);
        startButton = findViewById(R.id.start);
        stopButton = findViewById(R.id.stop);
        huifuButton = findViewById(R.id.huifu);
        if(aMap==null)aMap = mapView.getMap();
        MyLocationStyle myLocationStyle = new MyLocationStyle();
        myLocationStyle.interval(2000);
        myLocationStyle.strokeWidth(0f);
        myLocationStyle.radiusFillColor(Color.TRANSPARENT);
        myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE);
        aMap.setMyLocationStyle(myLocationStyle);
        aMap.moveCamera(CameraUpdateFactory.zoomTo(14f));
        addLine();
        startButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e("eeeee","start---->");
                startMove();
            }
        });
        stopButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e("eeeee","stop---->");
                smoothMarker.stopMove();
            }
        });
        huifuButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                smoothMarker.destroy();
                list.remove(0);
                startMove();
            }
        });



    }


    private void startMove() {
        // 构建 轨迹的显示区域
        LatLngBounds bounds = new LatLngBounds(list.get(0), list.get(list.size()-2));
        aMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 50));

        // 实例 SmoothMoveMarker 对象
        smoothMarker = new SmoothMoveMarker(aMap);
        // 设置 平滑移动的 图标
        smoothMarker.setDescriptor(BitmapDescriptorFactory.fromResource(R.drawable.icon_car));

        // 取轨迹点的第一个点 作为 平滑移动的启动
        LatLng drivePoint = list.get(0);
        Pair<Integer, LatLng> pair = SpatialRelationUtil.calShortestDistancePoint(list, drivePoint);
        list.set(pair.first, drivePoint);
        List<LatLng> subList = list.subList(pair.first, list.size());

        // 设置轨迹点
        smoothMarker.setPoints(subList);
        // 设置平滑移动的总时间  单位  秒
        smoothMarker.setTotalDuration(5);

        // 设置  自定义的InfoWindow 适配器
        aMap.setInfoWindowAdapter(new AMap.InfoWindowAdapter() {
            @Override
            public View getInfoWindow(Marker marker) {
                return getInfoWindowView(marker);
            }

            @Override
            public View getInfoContents(Marker marker) {
                return getInfoWindowView(marker);
            }
        });
        // 显示 infowindow
        smoothMarker.getMarker().showInfoWindow();
        // 开始移动
        smoothMarker.startSmoothMove();
    }

    private View getInfoWindowView(Marker marker) {
        return null;
    }

    private void addLine() {
        list = new ArrayList<>();
        list.add(startPoint);
        /*list.add(passPoint1);
        list.add(passPoint2);
        list.add(passPoint3);*/
        list.add(endPoint);
        List<Integer> colorList = new ArrayList<Integer>();
        List<BitmapDescriptor> bitmapDescriptors = new ArrayList<BitmapDescriptor>();
        int[] colors = new int[]{Color.argb(255, 0, 255, 0),Color.argb(255, 255, 255, 0),Color.argb(255, 255, 0, 0)};
        //用一个数组来存放纹理
        List<BitmapDescriptor> textureList = new ArrayList<>();
        textureList.add(BitmapDescriptorFactory.fromResource(R.drawable.custtexture));
        List<Integer> texIndexList = new ArrayList<Integer>();
        texIndexList.add(0);//对应上面的第0个纹理
        texIndexList.add(1);
        texIndexList.add(2);
        Random random = new Random();
        for (int i = 0; i < list.size(); i++) {
            colorList.add(colors[random.nextInt(3)]);
            bitmapDescriptors.add(textureList.get(0));
        }
        polyline = aMap.addPolyline(new PolylineOptions().setCustomTexture(BitmapDescriptorFactory.fromResource(R.drawable.custtexture)) //setCustomTextureList(bitmapDescriptors)
//				.setCustomTextureIndex(texIndexList)
                .addAll(list)
                .useGradient(true)
                .width(18));
        LatLngBounds bounds = new LatLngBounds(list.get(0), list.get(1));
        aMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 100));
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onResume() {
        super.onResume();
        mapView.onResume();

    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onPause() {
        super.onPause();
        mapView.onPause();

    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }
}
